<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属小区" prop="communityId">
        <el-select v-model="queryParams.communityId"   placeholder="请选择所属小区" filterable clearable size="small">
          <el-option
            v-for="community in communityOptions"
            :key="community.id"
            :label="community.name"
            :value="Number(community.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="门禁名称" prop="doorName">
        <el-input
          v-model="queryParams.doorName"
          placeholder="请输入门禁名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主姓名" prop="owerName">
        <el-input
          v-model="queryParams.owerName"
          placeholder="请输入业主姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:communityDoorUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="communityDoorUserList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门和用户关系表" align="center" prop="id" v-if="false"/>
      <el-table-column label="小区名称" align="center" prop="communityName" />
      <el-table-column label="开门名称" align="center" prop="doorName" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="业主姓名" align="center" prop="owerName" />
      <el-table-column label="门禁类型" align="center" prop="type" :formatter="deviceTypeFormat"/>
      <el-table-column label="权限" align="center" prop="status"   >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value= 1
            :inactive-value= 0
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createtime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatetime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listCommunityDoorUser, delCommunityDoorUser, exportCommunityDoorUser,changeDoorUserStatus } from "@/api/system/communityDoorUser";
import {getAllList} from "@/api/system/community";

export default {
  name: "CommunityDoorUser",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户门禁管理表格数据
      communityDoorUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 权限 0无权限  1有权限字典
      statusOptions: [],
      // 门禁门禁类型 1趣开门  2海康
      deviceTypeOptions: [],
      //小区下拉列表
      communityOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doorName: undefined,
        communityId: undefined,
        owerName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCommunityList();
    this.getDicts("community_door_user_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("community_door_device_type").then(response => {
      this.deviceTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询用户门禁管理列表 */
    getList() {
      this.loading = true;
      listCommunityDoorUser(this.queryParams).then(response => {
        this.communityDoorUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取小区列表
    getCommunityList(){
      let data={};
      getAllList(data).then(response => {
        this.communityOptions = response.data
      });
    },
    // 权限 0无权限  1有权限字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 设备类型：1趣开门，2海康字典翻译
    deviceTypeFormat(row, column) {
      return this.selectDictLabel(this.deviceTypeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        doorId: undefined,
        communityId: undefined,
        type: undefined,
        updatetime: undefined,
        createtime: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 状态修改
    handleStatusChange(row) {
      this.$confirm('确认要修改业主【"' + row.owerName + '"】的权限吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeDoorUserStatus(row.id,row.status);
      }).then(() => {
        this.msgSuccess("操作成功");
      }).catch(function() {
        row.status = row.status == 0 ? 1 : 0;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户门禁管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCommunityDoorUser(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户门禁管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCommunityDoorUser(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
